<?php
 /**
 +------------------------------------------------------------------------------
 * Html辅助类
 +------------------------------------------------------------------------------
 * @Version  3.0
 * @Author   WangXian
 * @E-mail    admin@loopx.cn
 * @package  others
 * @Creation  date 2010-10-17
 * @Modified  date 2010-12-25 19:02:12
 +------------------------------------------------------------------------------
 */
class Html
{
	/** html下拉框，选中 **/
	public static function selected($str1,$str2)
	{return $str1 == $str2 ? 'selected="selected"':'';}
	
	/**  html复选框，选中 **/
	public static function checked($str1,$str2)
	{return $str1 == $str2 ? 'checked="checked"':'';}
	
	/** html原样输出。 **/
	public static function H($str)
	{
		return htmlspecialchars($str);
	}
	
	/**
	 * 生成a链接，只能生成本站内的地址
	 * @param string $uri '控制器/action'
	 * @param string $text '链接文字'
	 */
	public static function a($uri, $text='')
	{
		$text = ($text == '') ? $uri : $text;
		return '<a href="'. U($uri) .'" title="">'. $text .'</a>';
	}
	
	/**
	 * 生成img的图片显示
	 * @param string $pic
	 * @param string $alt
	 */
	public static function img($pic, $alt='')
	{
		return '<img src="'. $pic .'" alt="'. $alt .'" />';
	}
	
	/**
	 * 输出n个html换行
	 * @param int $repeat
	 */
	public static function br($repeat = 1)
	{
		return str_repeat("<br />", $repeat);
	}
	
	/**
	 * 输出n个html空格
	 * @param int $repeat
	 */
	public static function nbs($repeat = 1)
	{
		return str_repeat("&nbsp;", $repeat);
	}
	
	/**
	 * 链接资源、css、js、ico
	 * @param $css
	 */
	public static function link_tag($source)
	{
		$ext = strrchr($source, '.');
		if($ext == '.css')
			return "<link href=\"{$source}\" rel=\"stylesheet\" type=\"text/css\" />";
		else if($ext == '.js' )
			return '<script type="text/javascript" src="'. $source .'"></script>';
		else if($ext == '.ico' )
			return '<link href="'. $source .'" rel="shortcut icon" type="image/ico" />';
	}
	
	/**
	 * 生成ul格式的html数据
	 * @param array $data
	 * @param array $attributes
	 * @return array
	 */
	public static function ul($data, $attributes=array())
	{
		if( ! is_array($data) ) return '';
		$attributes = $attributes + array('id'=>'', 'class'=>'');
		
		$str="<ul id=\"". $attributes['id'] ."\" class=\"". $attributes['class'] ."\">\n";
		foreach ($data as $k=>$v)
		{
			$str .= "\t<li>";
			$str .= is_array($v) ? $k.self::ul($v) : $v;
			$str .= "</li>\n";
		}
		$str .= "</ul>\n";
		return $str;
	}
	
	/**
	 * 生成ol格式的html数据
	 * @param array $data
	 * @param array $attributes
	 * @return array
	 */
	public static function ol($data, $attributes=array())
	{
		if( ! is_array($data) ) return '';
		$attributes = $attributes + array('id'=>'', 'class'=>'');
		
		$str="<ol id=\"". $attributes['id'] ."\" class=\"". $attributes['class'] ."\">\n";
		foreach ($data as $k=>$v)
		{
			$str .= "\t<li>";
			$str .= is_array($v) ? $k.self::ol($v) : $v;
			$str .= "</li>\n";
		}
		$str .= "</ol>\n";
		return $str;
	}
	
	
}
/* End of file Html.class.php */
/* Location: ./_framework/libraries/Html.class.php */